
let app = getApp()
Page({
 
  /**
   * 页面的初始数据
   */

  data: {
    showView:true,
    showsView:false,
    flag: false,
    sendmessg: "sendmessg",
    codeDis: false,
    clear_one: false,
    clear_two: false,
    getmsg: "获取短信验证码",
    telephone: "",
    codePhone: "",
    types:'password',
    active:"",
    types1: 'password',
    active1: "",
    types2: 'password',
    active2: "",
    unionId:'',
    content:'',
    contentOne:'',
    active:false,
    contentTwo:"",
    titles:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      unionId: wx.getStorageSync('openid'),
      titles: app.configData.titles
    })
    new app.ToastPannel();
  },
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView),
      showsView: (!that.data.showsView)
    })
  },
  onChangeShowStates: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView),
      showsView: (!that.data.showsView)
    })
  },
  //是否显示密码
  iconSee:function(e){
    
    var that = this 
    var types = ""
    var active = ""
    if (that.data.types == 'password'){
      types = "text"
      active = 'active'
    }else{
      types = 'password'
      active = ''
    }
    that.setData({
      types:types,
      active:active,
      one_content: that.data.content
    })
  },
text:function(e){
 
  this.setData({
    content: e.detail.value
  })
},
textOne: function (e) {
  
  this.setData({
    contentOne: e.detail.value
  })
},
textTwo: function (e) {
 
  this.setData({
    contentTwo: e.detail.value
  })
},
//显示密码
  iconSee1:function(e){
    
    var that = this
    var types = ""
    var active = ""
    if (that.data.types1 == 'password') {
      types = "text"
      active = "active"
    } else {
      types = 'password'
      active = ""
    }
    that.setData({
      types1: types,
      active1: active,
      two_content: that.data.contentOne
    })
  },
  iconSee2: function (e) {
    var that = this
    var types = ""
    var active = ""
    if (that.data.types2 == "password") {
      types = "text"
      active = "active"
    } else {
      types = 'password'
      active = ""
    }
    that.setData({
      types2: types,
      active2: active,
      three_content: that.data.contentTwo
    })
  },
  formSubmit: function (e) {
    var that = this;
    var re_user_mobile = that.data.telephone;
    var phoneCode = e.detail.value.phoneCode;
    var re_user_password = e.detail.value.re_user_password;
    var re_user_password2 = e.detail.value.re_user_password2;
    var url = app.configData.ucenter.api_url + '/index.php?ctl=Login&met=resetPassword&typ=json';
    var token = that.data.unionId;
    var rec = wx.getStorageSync('rec')
    if (re_user_mobile == '') {
      that.show('手机号码不得为空');     
    } else if (phoneCode == ''){
      that.show('验证码不能为空');     
    } else if (re_user_password == '') {
      that.show('登录密码不能为空');     
    } else if (re_user_password2 == '') {
      that.show('确认密码不能为空');   
    } else if (re_user_password2 != re_user_password) {
      that.show('确认密码与登录密码不符合');      
    }else {
      wx.request({
        url:url ,
        data: { mobile: re_user_mobile, code: phoneCode, password: re_user_password, token: token},
        method: 'POST', 
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }, // 设置请求的 header
        success: function (res) {
          if(res.data.status == 200){
			  wx.showModal({
				title: '提示',
				content: '重置密码成功，即将跳转页面',
				showCancel: false,
				success: (res) => {
				  if (res.confirm) {
					// wx.navigateTo({
					//   url: '/pages/select/select'
					// })
					wx.navigateBack({ delta: 1 });
				  }
				}
			  })
          }else{
            that.show(res.data.msg);
          }
        },
        fail: function (res) {
          that.show('请求失败');
        }
      })
    }
  },
 
//获取短信验证码
  sendmessg:function(e) {
    var that = this
    var timer = 1
    var telephone = that.data.telephone
    if (telephone.length != 11 || isNaN(telephone)) {
      that.show('请输入有效的手机号码');
      return
    }
    that.setData({
      sendmessg: ""
    })
    //发送短信验证码
    wx.request({
      url: app.configData.ucenter.api_url +'/index.php?ctl=Login&met=wxappregCode2&typ=json',
      data: {
        mobile: that.data.telephone
      },
      success: function (res) {
      
        if (res.data.status == 200){
          if (timer == 1) {
            timer = 0

            var time = 60
            

            var inter = setInterval(function () {
              that.setData({
                getmsg: time + "s后重新发送",
              })
              time--
              if (time < 0) {
                timer = 1
                clearInterval(inter)
                that.setData({
                  sendmessg: "sendmessg",
                  getmsg: "获取短信验证码",
                })
              }

            }, 1000)
          }
       }else{
          that.show(res.data.msg);
          that.setData({
            sendmessg: 'sendmessg'
          })
       }
      },
      fail:function(res){
        that.show('数据加载失败');
      }
    })


  },
  //手机号input
  phoneinput(e) {
    var that = this;  
    var value = e.detail.value
    if (value){
      that.setData({
        telephone: value,
        clear_one:true,
        active: true
      })
    }else{
      that.setData({
        clear_one: false,
        active:false
        
      })
    }
  },
    //短信验证码
  codeinput(e) {
    var that = this;
    var value = e.detail.value
    if (value) {
      that.setData({
        codePhone: value,
        clear_two: true
      })
    } else {
      that.setData({
        clear_two: false
      })
    }
  },
  //点击清空
  clear_one:function(){
    var that = this;
    that.setData({
      telephone: "",
      clear_one: false,
     
    })
  },
    //点击清空
  clear_tow: function () {
    var that = this;
    that.setData({
      codePhone: "",
      clear_two: false
    })
  },
})